package sg.edu.nus.iss.team8s.vmcs.control;

import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Label;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Observable;
import java.util.Observer;

import sg.edu.nus.iss.team8s.vmcs.gui.CustomerPanel;
import sg.edu.nus.iss.team8s.vmcs.state.StateFactory;
import sg.edu.nus.iss.team8s.vmcs.state.SystemState;
import sg.edu.nus.iss.team8s.vmcs.beans.DrinksBrand;
import sg.edu.nus.iss.team8s.vmcs.beans.DrinksStoreItem;
import sg.edu.nus.iss.team8s.vmcs.beans.StoreItem;
import sg.edu.nus.iss.team8s.vmcs.beans.StoreObject;
import sg.edu.nus.iss.team8s.vmcs.builder.BuilderFactory;
import sg.edu.nus.iss.team8s.vmcs.builder.UIBuilder;
import sg.edu.nus.iss.team8s.vmcs.builder.UIDirector;
import sg.edu.nus.iss.team8s.vmcs.cache.CashStore;
import sg.edu.nus.iss.team8s.vmcs.cache.DrinksStore;
/**
 * 
 * @author Nie XiaoYi, LU ZHIXIAN
 *
 */
public class TransactionController implements Observer{
	private static TransactionController instance = null;
	private int price = 0;
	private int identifier = 0;
	private CustomerPanel cusPanel;
	private SystemState state;
	
	
	private Button[] btnListCoin;
	private Button[] btnListDrink;
	private Label[] lblListDrinkPrice;
	private Label[] lblListStoreStatus;
	private Label totalMoneyValue;
	private Label collectCanValue;
	private Label collectCash;
	
	private TransactionController(){
		DrinksStore.getInstance().addObserver(this);
		CashStore.getInstance().addObserver(this);
	}
	
	public void updateDrinkSelection(int index){
		/*if(cusPanel != null){
			//cusPanel.getContentPane().refreshDrinksInventory(index);
			System.out.println("tran updateDrinkSelection started at " + new java.util.Date());
			cusPanel.removeAll();
			buildPanel();
			cusPanel.validate();
			System.out.println("tran updateDrinkSelection ended at " + new java.util.Date());
		}*/
		
		if(btnListDrink == null || lblListDrinkPrice == null || lblListStoreStatus == null)
			return;
		
		DrinksStore drinkStore = DrinksStore.getInstance();
		for(int i =0;i<drinkStore.getStoreSize();i++){
			StoreItem item = drinkStore.getItem(i);
			DrinksBrand obj = (DrinksBrand)item.getContent();
			if(item.getQuantity()<=0)
				btnListDrink[i].setEnabled(false);
			else
				btnListDrink[i].setEnabled(true);
			
			lblListDrinkPrice[i].setText(String.valueOf(obj.getPrice()));
			
			if(item.getQuantity()>0)
				lblListStoreStatus[i].setForeground(Color.GRAY);
			else
				lblListStoreStatus[i].setForeground(Color.WHITE);
			
		}
	}
	
	public static synchronized TransactionController getInstance(){
		if(instance == null){
			instance = new TransactionController();
		}
		return instance;
	}
	
	public void CloseCustomerPanel(){
		cusPanel.setVisible(false);
		
	}
	public void DisplayCustomerPanel(final SimulationController simController){
		cusPanel = new CustomerPanel();
		cusPanel.setSize(new Dimension(360,600));
		cusPanel.setResizable(false);
		StateFactory stateFactory = new StateFactory();
		cusPanel.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e) {
				StateFactory stateFactory = new StateFactory();
				setState(stateFactory.GetState(SystemState.CLOSINGSTATE));
				System.out.println("The current state is ClosingState!");
				simController.setActive(true, '1');
				processState();
			}
		});
	
		
		setState( stateFactory.GetState(SystemState.STARTINGSTATE));
		System.out.println("The current state is StartingState!");
		processState();
	}
	
	public void ShowCustomerPanel(){
		buildPanel();
		cusPanel.display();		
	}
	
	private void buildPanel(){
		UIBuilder builder = BuilderFactory.getInstance().getUIBuilder("customerUIBuilder");
		UIDirector uiDir = new UIDirector(builder);
		uiDir.build();
		cusPanel.add(builder.getResult());
		cusPanel.setContentPane(builder.getResult());
	}

	public void update(Observable arg0, Object arg1) {
		// TODO Auto-generated method stub
		updateDrinkSelection(0);
	}
	
	public void startTransaction(){
		DrinksStore drinks = DrinksStore.getInstance();
		StoreItem item = drinks.getItem(identifier);
		StoreObject drink = item.getContent();
		price = ((DrinksBrand)drink).getPrice();
		/*CoinReceiver coinReceiver = CoinReceiver.getInstance();
		coinReceiver.StartReceive();*/
		setCoinInputBoxStatus(true);
	}
	
	public void setState(SystemState state){
		this.state = state;
	}
	
	public void processState(){
		state.processState();
	}
	
	
	
	public boolean processMoneyReceived(int value){
		if(value >= price){
			setCoinInputBoxStatus(false);
			return true;
		}
		else{
			/*CoinReceiver coinReceiver = CoinReceiver.getInstance();
			coinReceiver.continueReceive();*/
			return false;
		}
			
	}
	
	public void setSelectDrinkIndex(int identifier){
		this.identifier = identifier;
	}
	
	public int getSelectDrinkIndex(){
		return this.identifier;
	}
	
	public int getPrice(){
		return this.price;
	}
	
	public void setLabelCollectCanValue(Label collectCan){
		this.collectCanValue = collectCan;
	}
	
	public void setLabelCollectCanValueText(String value){
		if(collectCanValue == null)
			return;
		this.collectCanValue.setText(value);
	}
	
	public void setCoinInputBox(Button[] btnListCoin){
		this.btnListCoin = btnListCoin;
	}
	
	public void setTotalMoneyValue(Label totalMoneyValue){
		this.totalMoneyValue = totalMoneyValue;
	}
	
	public int getTotalMoneyValue(){
		return Integer.parseInt(this.totalMoneyValue.getText());
	}
	
	public void setCoinInputBoxStatus(boolean status){
		for(int i =0 ; i<btnListCoin.length;i++){
			btnListCoin[i].setEnabled(status);
		}
	}
	
	public void setTotalMoneyValueText(String value){
		if(totalMoneyValue == null)
			return;
		this.totalMoneyValue.setText(value);
	}
	
	public void setDrinkSelectBtn(Button[] btnListDrink){
		this.btnListDrink = btnListDrink;
	}
	
	public void setDrinkPrice(Label[] lblListDrinkPrice){
		this.lblListDrinkPrice = lblListDrinkPrice;
	}
	
	public void setStoreStatus(Label[] lblListStoreStatus){
		this.lblListStoreStatus = lblListStoreStatus;
	}
	
	public void setCollectCash(Label collectCash){
		this.collectCash = collectCash;
	}
	
	public void setCollectCashText(String value){
		if(collectCash == null)
			return;
		this.collectCash.setText(value);
	}
}
